******************************************************************************************
**** Cropping frequency and area response to climate shocks can exceed yield response ****
******************************************************************************************
****************************    ANALYSIS   ***********************************************
clear
use mcaFINAL_with_avgs.dta, clear
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
foreach DV in "agm" "aga" "dcm" "dca" {
use mcaFINAL_with_avgs.dta
quietly reg `DV' t_* st_* sp_* p_*   pa_* r_`DV' dyear* dmca*, cluster(mca)
clear
use mcaFINAL_with_avgs.dta
foreach month in "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec" {
replace p_`month' = `month'aprec * (`dp'/10)
replace sp_`month' = (`month'aprec * (`dp'/10))^2
}
foreach month in "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec" {
quietly replace t_`month' = `month'atemp + (`dt'-3)
quietly replace st_`month' = (`month'atemp +(`dt'-3))^2
}
quietly predict `DV'p`dp't`dt'
sort mcacode year
save `DV'p`dp't`dt',replace
}
}
}
clear
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
foreach DV in "agm" "aga" "dcm" "dca" {
use `DV'p`dp't`dt'.dta
quietly keep mcacode year `DV'p`dp't`dt'
save `DV'p`dp't`dt'.dta, replace
}
}
}
clear
use mcaFINAL_with_avgs.dta, clear
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
foreach DV in "agm" "aga" "dcm" "dca" {
quietly merge 1:1 mcacode year using `DV'p`dp't`dt'.dta, nogen
}
}
}
save mcaFINAL4.dta, replace
clear
use mcaFINAL_with_avgs.dta, clear
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
foreach DV in "soy" "corn2"  {
use mcaFINAL_with_avgs.dta
quietly reg `DV' t_* st_* sp_* p_* pa_* dyear* dmca* , cluster(mca)
clear
use mcaFINAL_with_avgs.dta
foreach month in "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec" {
quietly replace p_`month' = `month'aprec *(`dp'/10)
quietly replace sp_`month' = (`month'aprec * (`dp'/10))^2
}
foreach month in "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec" {
quietly replace t_`month' = `month'atemp + (`dt'-3)
quietly replace st_`month' = (`month'atemp +(`dt'-3))^2
}
quietly predict `DV'p`dp't`dt'
sort mcacode year
save `DV'p`dp't`dt',replace
}
}
}
clear
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
foreach DV in "soy" "corn2" {
use `DV'p`dp't`dt'.dta
quietly keep mcacode year `DV'p`dp't`dt'
save `DV'p`dp't`dt'.dta, replace
}
}
}
clear
use mcaFINAL4.dta, clear
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
foreach DV in "soy" "corn2" {
quietly merge 1:1 mcacode year using `DV'p`dp't`dt'.dta, nogen
}
}
}
save mcaFINAL_w_pred_june.dta, replace
use mcaFINAL_w_pred_june.dta
sort mcacode
quietly keep if year==2010
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
foreach DV in "agm" "aga" "dcm" "dca" {
quietly replace `DV'p`dp't`dt' = 0 if `DV'p`dp't`dt'<0
quietly replace `DV'p`dp't`dt' = `DV'p`dp't`dt'*6.25
}
}
}
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
foreach DV in "agm" "aga" {
quietly gen `DV'tonsp`dp't`dt' = `DV'p`dp't`dt'*2500*2.2
}
foreach DV in "dca" "dcm" {
quietly gen `DV'tonsp`dp't`dt' = `DV'p`dp't`dt'*2900
}
}
}
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
quietly gen soytonsp`dp't`dt' = soyp`dp't`dt'*agr2010*2.2
quietly gen corn2tonsp`dp't`dt' = corn2p`dp't`dt'*dcr2010
}
}
keep if mcacode<200
quietly collapse (sum) agmtons* agatons* dcatons* dcmtons* soytons* corn2tons*
save statelevel2.dta, replace
clear
use statelevel2.dta
foreach DV in "agm" "aga" "dcm" "dca" {
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
quietly gen `DV'net`dp't`dt'=`DV'tonsp`dp't`dt'-`DV'tonsp10t3
}
}
}
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
quietly gen yield1net`dp't`dt'=soytonsp`dp't`dt'- soytonsp10t3
quietly gen yield2net`dp't`dt'=corn2tonsp`dp't`dt'- corn2tonsp10t3
}
}
gen totalprod=45000000000
drop soy* corn* agmtons* dcmtons* agatons* dcatons*
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
quietly capture gen allyieldnet`dp't`dt'=yield1net`dp't`dt'+yield2net`dp't`dt'
quietly gen alldoublenet`dp't`dt'=dcmnet`dp't`dt'-dcanet`dp't`dt'+ yield2net`dp't`dt'
quietly gen allsinglenet`dp't`dt'=agmnet`dp't`dt'-aganet`dp't`dt'+ yield1net`dp't`dt'
quietly gen allfreqnet`dp't`dt'=dcmnet`dp't`dt'-dcanet`dp't`dt'
quietly gen allcornyield`dp't`dt'=yield2net`dp't`dt'
quietly gen allsoyyield`dp't`dt'=yield1net`dp't`dt'
}
}
drop yield*
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
quietly gen allareanet`dp't`dt'=dcmnet`dp't`dt'+agmnet`dp't`dt' - dcanet`dp't`dt'- aganet`dp't`dt'
quietly gen totalnet`dp't`dt'=allareanet`dp't`dt'+allyieldnet`dp't`dt'
quietly gen intnet`dp't`dt'=dcmnet`dp't`dt'+allyieldnet`dp't`dt'-dcanet`dp't`dt'
quietly gen extnet`dp't`dt'=agmnet`dp't`dt'- aganet`dp't`dt'
quietly gen zdcanet`dp't`dt'=-1*dcanet`dp't`dt'
quietly gen zdcmnet`dp't`dt'=dcmnet`dp't`dt'
}
}
drop  dcm* dca* agm* aga*
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
quietly gen PERyield`dp't`dt'=(allyieldnet`dp't`dt')/(totalprod)
quietly gen PERarea`dp't`dt'=(allareanet`dp't`dt')/(totalprod)
quietly gen PERfreq`dp't`dt'=(allfreqnet`dp't`dt')/(totalprod)
quietly gen PERcornyield`dp't`dt'=(allcornyield`dp't`dt')/(totalprod)
quietly gen PERsoyyield`dp't`dt'=(allsoyyield`dp't`dt')/(totalprod)
quietly gen PERint`dp't`dt'=(intnet`dp't`dt')/(totalprod)
quietly gen PERext`dp't`dt'=(extnet`dp't`dt')/(totalprod)
quietly gen PERdca`dp't`dt'=(zdcanet`dp't`dt')/(totalprod)
quietly gen PERdcm`dp't`dt'=(zdcmnet`dp't`dt')/(totalprod)
}
}
drop allyield* allarea* allfreq* int* ext* z*
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
quietly gen PERdouble`dp't`dt'=(alldoublenet`dp't`dt')/(totalprod)
quietly gen PERsingle`dp't`dt'=(allsinglenet`dp't`dt')/(totalprod)
quietly gen PERtotal`dp't`dt'=(totalnet`dp't`dt')/(totalprod)
}
}
drop all* total*
save results.dta, replace
rename PER* *
quietly gen statecode=1
quietly gen sampcode=1
quietly reshape long dcm7t dca7t cornyield7t soyyield7t freq7t yield7t area7t double7t single7t total7t dcm8t dca8t dcm13t dca13t cornyield8t soyyield8t freq8t yield8t area8t double8t single8t total8t  dcm9t dca9t cornyield9t soyyield9t freq9t yield9t area9t double9t single9t total9t  dcm10t dca10t cornyield10t soyyield10t freq10t yield10t area10t double10t single10t total10t  dcm11t dca11t  cornyield11t soyyield11t freq11t yield11t area11t double11t single11t total11t  dcm12t dca12t cornyield12t soyyield12t freq12t yield12t area12t double12t single12t total12t cornyield13t soyyield13t freq13t yield13t area13t double13t single13t total13t int7t int8t int9t int10t int11t int12t int13t ext7t ext8t ext9t ext10t ext11t ext12t ext13t, i(sampcode) j(tempcode)
rename *t *
drop statecode
drop sampcode
rename *7* *1Minus30*
rename *8* *2Minus20*
rename *10* *4Zero*
rename *9* *3Minus10*
rename *11* *5Plus10*
rename *12* *6Plus20*
rename *13* *7Plus30*
order _all, alphabetic
order tempcode, first
rename *Minus* Minus*
rename *Plus* Plus*
rename *Zero* Zero*
xpose, clear varname
order _
rename v1 minus3
rename v2 minus2
rename v3 minus1
rename v4 zero
rename v5 plus1
rename v6 plus2
rename v7 plus3
drop in 1
drop *2 *3
rename _ precipScenario
replace precipScenario = subinstr(precipScenario,"Minus","",.)
replace precipScenario = subinstr(precipScenario,"Plus","",.)
replace precipScenario = subinstr(precipScenario,"Zero","",.)
replace precipScenario = subinstr(precipScenario,"3","8",.)
replace precipScenario = subinstr(precipScenario,"1","-30%P",.)
replace precipScenario = subinstr(precipScenario,"2","-20%P",.)
replace precipScenario = subinstr(precipScenario,"8","-10%P",.)
replace precipScenario = subinstr(precipScenario,"4","noChangeP",.)
replace precipScenario = subinstr(precipScenario,"5","+10%P",.)
replace precipScenario = subinstr(precipScenario,"6","+20%P",.)
replace precipScenario = subinstr(precipScenario,"7","+30%P",.)
rename precipScenario PrecipScenario
rename minus* minus*0
rename plus* plus*0
rename zero zeroT
rename minus* minus*T
rename plus* plus*T
save results.dta, replace
forvalues dp = 7(1)13  {
forvalues dt = 0(1)6  {
foreach DV in "agm" "aga" "dcm" "dca" "soy" "corn2" {
erase `DV'p`dp't`dt'.dta
}
}
}
erase statelevel2.dta
erase mcaFINAL_w_pred_june.dta
erase mcaFINAL4.dta
erase mcaFINAL_with_avgs.dta
erase data2.dta
